package com.ysw.wisdomcampus

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import androidx.viewpager.widget.ViewPager
import com.ysw.wisdomcampus.fragment.MyPerformanceFragment
import com.ysw.wisdomcampus.fragment.PersonalFragment
import com.ysw.wisdomcampus.fragment.SubjectFragment
import com.ysw.wisdomcampus.model.projectModel
import com.zhuangfei.timetable.core.TimetableView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {

    private val tabs = arrayOf("课程表", "成绩", "信息")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tool.text = "${projectModel.activeStudent!!.name}的${tabs[0]}"
        done.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                projectModel.toast("查询签到列表...")
                val dones = withContext(Dispatchers.IO) {
                    val infos = arrayListOf<String>()
                    for(item in projectModel.queryDone()) {
                        infos.add("${item.name}@${item.room}, ${item.time}")
                    }
                    return@withContext infos
                }
                if (dones.size != 0) {
                    val view = LayoutInflater.from(this@MainActivity).inflate(R.layout.done, null)
                   AlertDialog.Builder(this@MainActivity).setView(view).create().apply {
                        view.findViewById<TextView>(R.id.title).text = "签到记录"
                        view.findViewById<TextView>(R.id.confirm).setOnClickListener { dismiss() }
                       view.findViewById<ListView>(R.id.listview).apply {
                           adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_list_item_1, dones)
                       }
                        show()
                        window!!.setLayout(
                            this@MainActivity.resources.displayMetrics.widthPixels - 30,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                    }
                } else {
                    projectModel.toast("你还没有签到任何课程哦！")
                }
            }
        }
        vp.offscreenPageLimit = 3
        vp.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                tool.text = "[${projectModel.activeStudent!!.name}]的${tabs[position]}"
            }
        })
        tablayout.setViewPager(
            vp,
            tabs,
            this,
            arrayListOf(
                SubjectFragment(),
                MyPerformanceFragment(),
                PersonalFragment()
            )
        )
    }
}